package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.KeyAreaSearchBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 重点区域查询
 */
public class KeyAreaSearchActivity extends BaseActivity implements IPublicBeanView{

    @BindView(R.id.et_KeyAreaName) EditText et_KeyAreaName;
    @BindView(R.id.sp_KeyAreaType) Spinner sp_KeyAreaType;
    @BindView(R.id.et_AreaAddress) EditText et_AreaAddress;
    @BindView(R.id.et_Header) EditText et_Header;
    @BindView(R.id.et_Telephone) EditText et_Telephone;
    @BindView(R.id.et_Phone) EditText et_Phone;
    @BindView(R.id.rbt_Yes) RadioButton rbt_Yes;


    private List<GlobalBean> areaType=new ArrayList<>();

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_key_area_search);
        ButterKnife.bind(this);
        setTitleName("重点区域查询");
        setTitleRight(R.string.search);
        initData();
    }

    private void initData() {
        PublicListPest publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("区域类别");
    }

    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right://提交
                submit();
                break;
        }
    }

    private void submit() {

        String areaName=et_KeyAreaName.getText().toString().trim();
        String areaAddress=et_AreaAddress.getText().toString().trim();
        String header=et_Header.getText().toString().trim();
        String telephone=et_Telephone.getText().toString().trim();
        String phone=et_Phone.getText().toString().trim();
        String typeId="";
        GlobalBean typeBean=(GlobalBean)sp_KeyAreaType.getSelectedItem();
        if (typeBean!=null){
            typeId=(typeBean.getId()).equals("-1") ?"":typeBean.getId();
        }
        boolean isDanger=rbt_Yes.isChecked();
        KeyAreaSearchBean searchBean=new KeyAreaSearchBean();
        searchBean.setAreaName(areaName);
        searchBean.setAreaAddress(areaAddress);
        searchBean.setHeader(header);
        searchBean.setTelephone(telephone);
        searchBean.setPhone(phone);
        searchBean.setAreaType(typeId);
        searchBean.setDanger(isDanger);
        MyRxBus.get().post(new MsgEvent<>("KeyAreaActivity",2,searchBean));
        finish();
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("区域类别".equals(publicBean.getDomainName())){
            areaType=publicBean.getDicts();
            areaType.add(0,new GlobalBean("未选择","-1"));
            if (areaType!=null){
                SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,areaType);
                sp_KeyAreaType.setAdapter(adapter);
            }
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }
}
